package com.share.shareplatform.util;



import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class TokenUtil {
    public static final String[] authorities={"Find","Add","Update","Delete","Super"};
    public static final String username = "system";
    public static final Long id=-1l;
    public static String createTempToken(String moduleName){
        StringBuilder authorityBuilder=new StringBuilder();
        for(int i=0;i<authorities.length;i++) {
            authorityBuilder.append(moduleName);
            authorityBuilder.append(authorities[i]);
            authorityBuilder.append(",");
        }
        //删除最后一个
        authorityBuilder.deleteCharAt(authorityBuilder.length()-1);

        String token =  Jwts.builder()
                //主题 放入用户名
                .setSubject(username)
                //自定义属性 放入用户拥有请求权限
                .claim(Constants.Jwt.AUTHORITIES, authorityBuilder.toString())
                .claim("id",id+"")
                //失效时间
                .setExpiration(new Date(System.currentTimeMillis() + 60* 60 * 1000))
                //签名算法和密钥
                .signWith(SignatureAlgorithm.HS512, Constants.Jwt.KEY)
                .compact();
        System.out.println(token);
        return token;
    }
}
